package cloud.shyidong.admin.system.service;

import cloud.shyidong.core.request.LongIdRequest;
import cloud.shyidong.core.service.AbstractService;
import cloud.shyidong.core.util.CollectionUtils;
import cloud.shyidong.system.dao.*;
import cloud.shyidong.system.entity.*;
import cloud.shyidong.system.request.SystemRoleInsertRequest;
import cloud.shyidong.system.request.SystemRolePageRequest;
import cloud.shyidong.system.request.SystemRoleUpdateRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表业务处理
 * </p>
 *
 * @author syd
 * @since 2023-05-31 13:14:46
 */
@Service
public class SystemRoleService extends AbstractService<SystemRoleDao, SystemRoleEntity, LongIdRequest, SystemRoleInsertRequest, SystemRoleUpdateRequest, SystemRolePageRequest> {
    
    private SystemMenuDao systemMenuDao;
    
    private SystemFunctionDao systemFunctionDao;
    
    private SystemRoleMenuDao systemRoleMenuDao;
    
    private SystemRoleFunctionDao systemRoleFunctionDao;
    
    @Autowired
    public void setSystemMenuDao(SystemMenuDao systemMenuDao) {
        this.systemMenuDao = systemMenuDao;
    }
    
    @Autowired
    public void setSystemFunctionDao(SystemFunctionDao systemFunctionDao) {
        this.systemFunctionDao = systemFunctionDao;
    }
    
    @Autowired
    public void setSystemRoleMenuDao(SystemRoleMenuDao systemRoleMenuDao) {
        this.systemRoleMenuDao = systemRoleMenuDao;
    }
    
    @Autowired
    public void setSystemRoleFunctionDao(SystemRoleFunctionDao systemRoleFunctionDao) {
        this.systemRoleFunctionDao = systemRoleFunctionDao;
    }
    
    @Override
    public void afterInsert(SystemRoleInsertRequest insertRequest, SystemRoleEntity systemRoleEntity) {
        
        insertMenu(systemRoleEntity.getId(), insertRequest.getMenuIds());
        
        insertFunction(systemRoleEntity.getId(), insertRequest.getFunctionIds());
        
    }
    
    @Override
    public void afterUpdate(SystemRoleUpdateRequest updateRequest, SystemRoleEntity systemRoleEntity) {
        
        systemRoleMenuDao.delete(new UpdateWrapper<SystemRoleMenuEntity>().eq("role_id", systemRoleEntity.getId()));
        
        insertMenu(systemRoleEntity.getId(), updateRequest.getMenuIds());
        
        systemRoleFunctionDao.delete(new UpdateWrapper<SystemRoleFunctionEntity>().eq("role_id", systemRoleEntity.getId()));
        
        insertFunction(systemRoleEntity.getId(), updateRequest.getFunctionIds());
        
    }
    
    private void insertMenu(Long id, List<Long> menuIds) {
        
        if (CollectionUtils.isEmpty(menuIds)) {
            return;
        }
        
        for (Long menuId : menuIds) {
            SystemRoleMenuEntity systemRoleMenuEntity = new SystemRoleMenuEntity();
            systemRoleMenuEntity.setRoleId(id);
            systemRoleMenuEntity.setMenuId(menuId);
            systemRoleMenuDao.insert(systemRoleMenuEntity);
        }
        
    }
    
    private void insertFunction(Long id, List<Long> functionIds) {
        
        if (CollectionUtils.isEmpty(functionIds)) {
            return;
        }
        
        for (Long functionId : functionIds) {
            SystemRoleFunctionEntity systemRoleFunctionEntity = new SystemRoleFunctionEntity();
            systemRoleFunctionEntity.setRoleId(id);
            systemRoleFunctionEntity.setFunctionId(functionId);
            systemRoleFunctionDao.insert(systemRoleFunctionEntity);
        }
        
    }
    
    @Override
    public Map<String, Object> view(LongIdRequest idRequest) {
        
        Map<String, Object> result = super.view(idRequest);
        List<SystemRoleMenuEntity> systemRoleMenuEntities = systemRoleMenuDao.selectList(new QueryWrapper<SystemRoleMenuEntity>().eq("role_id", idRequest.getId()));
        if (CollectionUtils.isNotEmpty(systemRoleMenuEntities)) {
            List<SystemMenuEntity> systemMenuEntities = new ArrayList<>();
            for (SystemRoleMenuEntity systemRoleMenuEntity : systemRoleMenuEntities) {
                SystemMenuEntity systemMenuEntity = systemMenuDao.selectById(systemRoleMenuEntity.getMenuId());
                systemMenuEntities.add(systemMenuEntity);
            }
            result.put("menus", systemMenuEntities);
            
        }
        List<SystemRoleFunctionEntity> systemRoleFunctionEntities = systemRoleFunctionDao.selectList(new QueryWrapper<SystemRoleFunctionEntity>().eq("role_id", idRequest.getId()));
        if (CollectionUtils.isNotEmpty(systemRoleFunctionEntities)) {
            List<SystemFunctionEntity> systemFunctionEntities = new ArrayList<>();
            for (SystemRoleFunctionEntity systemRoleFunctionEntity : systemRoleFunctionEntities) {
                SystemFunctionEntity systemFunctionEntity = systemFunctionDao.selectById(systemRoleFunctionEntity.getFunctionId());
                systemFunctionEntities.add(systemFunctionEntity);
            }
            result.put("functions", systemFunctionEntities);
        }
        return result;
        
    }
    
}
